﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BOSS.Service.Caching;

namespace BOSS.Service.Implements.Caching
{
    /// <summary>
    /// 基于静态字典的缓存服务实现
    /// </summary>
    public class StaticDictionaryCacheService : ICacheService
    {
        /// <summary>
        /// 内部缓存实现
        /// Key = 分类名称
        /// Value = 分类缓存
        /// </summary>
        private IDictionary<string, object> m_categoryCaches;

        /// <summary>
        /// 构造函数
        /// </summary>
        public StaticDictionaryCacheService()
        {
            this.m_categoryCaches = new Dictionary<string, object>();
        }

        #region ICacheService 成员

        /// <summary>
        /// 获取指定名称的分类缓存
        /// </summary>
        /// <typeparam name="TKey"></typeparam>
        /// <typeparam name="TValue"></typeparam>
        /// <param name="categoryName"></param>
        /// <returns></returns>
        public ICategoryCache<TKey, TValue> GetCategoryCache<TKey, TValue>(string categoryName)
        {
            if (this.m_categoryCaches.ContainsKey(categoryName))
            {
                return this.m_categoryCaches[categoryName] as ICategoryCache<TKey, TValue>;
            }
            else
            {
                ICategoryCache<TKey, TValue> categoryCache = new StaticDictionaryCategoryCache<TKey, TValue>(categoryName);
                this.m_categoryCaches[categoryName] = categoryCache;
                return categoryCache;
            }
        }

        #endregion
    }
}
